package com.brzjomo.embraceofthevoid;

import com.brzjomo.embraceofthevoid.init.*;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class EmbraceOfTheVoid implements ModInitializer {
    // This logger is used to write text to the console and the log file.
    // It is considered best practice to use your mod id as the logger's name.
    // That way, it's clear which mod wrote info, warnings, and errors.
    public static final Logger LOGGER = LoggerFactory.getLogger("embraceofthevoid");
    public static final String MOD_ID = "embraceofthevoid";
    public static final boolean IS_DEBUG = false;

    @Override
    public void onInitialize() {
        // This code runs as soon as Minecraft is in a mod-load-ready state.
        // However, some things (like resources) may still be uninitialized.
        // Proceed with mild caution.

        // Register Blocks
        for (Block i : ModBlocks.BLOCK_LIST) {
            Registry.register(Registries.BLOCK, ModBlocks.getId(i), i);
        }

        // Register Block Items
        for (Item i : ModItems.BLOCK_ITEM_LIST) {
            Registry.register(Registries.ITEM, ModItems.getId(i), i);
        }

        // Register Block Entities
        for (BlockEntityType<?> i : ModBlockEntityTypes.BLOCK_ENTITY_List) {
            Registry.register(Registries.BLOCK_ENTITY_TYPE, ModBlockEntityTypes.getId(i), i);
        }

        // Register Items
        for (Item i : ModItems.ITEM_LIST) {
            Registry.register(Registries.ITEM, ModItems.getId(i), i);
        }

        // Register Item Groups
        for (ItemGroup i : ModItemGroups.MOD_ITEM_GROUP_LIST) {
            Registry.register(Registries.ITEM_GROUP, ModItemGroups.getId(i), i);

            // Modify Item Groups
            if (Objects.equals(ModItemGroups.getPath(i), ModItemGroups.MOD_GROUP_MAIN_PATH)) {
                ItemGroupEvents.modifyEntriesEvent(RegistryKey.of(RegistryKeys.ITEM_GROUP, new Identifier(EmbraceOfTheVoid.MOD_ID, Objects.requireNonNull(ModItemGroups.getPath(i))))).register(content -> {
                    content.add(ModItems.NBT_STICK);
                    content.add(ModItems.NIGHTFALL_TELEPORT_GEM);
                    content.add(ModItems.BLESS_TELEPORT_GEM);
                    content.add(ModItems.TELEPORT_GEM_CORE);
                    content.add(ModItems.VILLAGER_ALTAR);
                });
            }
        }

        // Register nightfall gem to existing loot table
        for (Identifier i : ModLootTables.LOOT_TABLE_INSERT_NIGHTFALL_TELEPORT_GEM_ID_LIST) {
            lootTableInsert(i, ModItems.NIGHTFALL_TELEPORT_GEM, 0.35F);
        }

        // Register bless gem to existing loot table
        for (Identifier i : ModLootTables.LOOT_TABLE_INSERT_BLESS_TELEPORT_GEM_ID_LIST) {
            lootTableInsert(i, ModItems.BLESS_TELEPORT_GEM, 0.15F);
        }

        // Register teleport gem core to existing loot table
        for (Identifier i : ModLootTables.LOOT_TABLE_INSERT_AMETHYST_BUD_AND_CLUSTER_ID_LIST) {
            if (i.equals(ModLootTables.AMETHYST_CLUSTER_ID)) {
                lootTableInsert(i, ModItems.TELEPORT_GEM_CORE, 0.35F);
            } else if (i.equals(ModLootTables.LARGE_AMETHYST_BUD_ID)) {
                lootTableInsert(i, ModItems.TELEPORT_GEM_CORE, 0.25F);
            } else if (i.equals(ModLootTables.MEDIUM_AMETHYST_BUD_ID)) {
                lootTableInsert(i, ModItems.TELEPORT_GEM_CORE, 0.15F);
            } else if (i.equals(ModLootTables.SMALL_AMETHYST_BUD_ID)) {
                lootTableInsert(i, ModItems.TELEPORT_GEM_CORE, 0.05F);
            }
        }
    }

    public void lootTableInsert(Identifier i, LootPool.Builder builder) {
        LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
            if (source.isBuiltin() && i.equals(id)) {
                tableBuilder.pool(builder);
            }
        });
    }

    public void lootTableInsert(Identifier i, Item item, float possibility) {
        LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
            if (source.isBuiltin() && i.equals(id)) {
                LootPool.Builder poolBuilder = LootPool.builder().conditionally(RandomChanceLootCondition.builder(possibility))
                        .with(ItemEntry.builder(item));
                tableBuilder.pool(poolBuilder);
            }
        });
    }
}